package Tydzien8;

import java.awt.CheckboxMenuItem;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MenuFrame extends Frame implements ActionListener {

	private static final long serialVersionUID = 6891845336952724588L;
	
	Menu Menu1, SubMenu1;                                                  
    MenuBar Menubar1;                                                      
    TextField text1;                                                 
    MenuItem[] menuItems;                                                                    
                                                                                                                                  
    MenuFrame(String title)
    {                                               
    	super(title); 
    	menuItems = new MenuItem[4];
        text1 = new TextField("Full menu");                      
        setLayout(new GridLayout(1, 1));                               
        add(text1);                                              
        Menubar1 = new MenuBar();                                      
        Menu1 = new Menu("File");                                      
           
        for(int i = 0; i < 3; ++i)
        {
        	if(i != 2) menuItems[i] = new MenuItem("Item " + (i+1));                            
        	else menuItems[i] = new CheckboxMenuItem("Check Item");                
        	menuItems[i].addActionListener(this);                             
            Menu1.add(menuItems[i]); 
            if(i > 0) Menu1.addSeparator();
        }
        
        SubMenu1 = new Menu("Sub menus");                              
        SubMenu1.add(new MenuItem("Hello"));                           
        SubMenu1.add(new MenuItem("from"));                            
        SubMenu1.add(new MenuItem("Java"));                            
                                                                                                                              
        Menu1.add(SubMenu1);                                           
        Menubar1.add(Menu1);                                           
        setMenuBar(Menubar1);                                          
                                                                                                                              
        Menu1.addSeparator();                                          
                                                                                                                              
        menuItems[3] = new MenuItem("Exit");                              
        menuItems[3].addActionListener(this);                             
        Menu1.add(menuItems[3]);                                              
    }                                                                      
                                                                                                                                  
    public void actionPerformed(ActionEvent event){ 
    	int index = -1;
    	for(int i = 0; i < 4; ++i)
    	{
    		if(event.getSource() == menuItems[i])
    		{
    			index = i;
    			break;
    		}
    	}
    	
    	switch(index)
    	{
    		case 0: text1.setText("Item 1"); break;
    		case 1:
    		{
    			menuItems[1].setEnabled(false);                                 
                text1.setText("Item 2");
                break;
    		}
    		case 2: ((CheckboxMenuItem)event.getSource()).setState(true); break;
    		case 3: setVisible(false); break;
    		default: break;
    	}                                                                                                                                        
    }                                                              
}
